﻿Imports Exp.Model
Imports Exp.DATA_Service

Public Class Profile
    Inherits BasePage


    Const MENUNAME As String = "PROFILE"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                Dim objUser As REM_User = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

                BindControl(objUser)

            End If
        End If
    End Sub

    Private Sub BindControl(user As REM_User)

        lblUserName.Text = user.UserName
        lblFName.Text = user.FirstName
        lblLName.Text = user.LastName
        lblMobile.Text = user.MobilePhone
        lblPhone.Text = user.HomePhone
        lblEmail.Text = user.Email
        lblCreditCard.Text = user.CreditcardNumber
        lblAddress.Text = user.Address
        lblLastLogin.Text = If(user.LastLoginDate IsNot Nothing, user.LastLoginDate.Value.ToString("dd MMM yyyy"), "-")
        lblJoinDate.Text = If(user.JoinDate IsNot Nothing, user.JoinDate.Value.ToString("dd MMM yyyy"), "-")
        lblExpireDate.Text = If(user.ExpireDate IsNot Nothing, user.ExpireDate.Value.ToString("dd MMM yyyy"), "-")

        If user.Role_ID = 3 Then
            Dim usrService As New User_Service()
            grdData.DataSource = usrService.getOwnerDetail(user.User_ID)
            grdData.DataBind()

            Dim point As Decimal = GetPoint()
            Dim usedP As Decimal = usrService.GetUsagePoint(user.User_ID)
            Dim balnc As Decimal = point - usedP

            lblPoint.Text = point.ToString("#,###")
            lblUsedPoint.Text = usedP.ToString("#,###")
            lblBalance.Text = balnc.ToString("#,###")
        Else
            divBalancePoint.Visible = False            
            divOwner.Visible = False
            divPoint.Visible = False
            divUsedPoint.Visible = False
        End If
    End Sub

    Protected Sub lnkChangePass_Click(sender As Object, e As EventArgs) Handles lnkChangePass.Click
        Response.Redirect("ChangePassword.aspx")
    End Sub

    Private Function GetPoint() As Decimal
        Dim res As Decimal = 0
        For Each row As EXP_Owner_detail In grdData.DataSource
            res = res + row.Total
        Next
        Return res
    End Function

    Private Sub grdData_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdData.RowCommand
        Dim Unit_ID As Integer = CInt(e.CommandArgument.ToString)
        If e.CommandName.ToUpper = "VIEWDETAIL" Then
            multiview1.ActiveViewIndex = 1
            Me.ucUnitView1.UnitID = Unit_ID
            Me.ucUnitView1.BindData()
        End If
    End Sub

    Private Sub btnBackView1_Click(sender As Object, e As EventArgs) Handles btnBackView1.Click
        multiview1.ActiveViewIndex = 0
    End Sub

    Protected Sub btnBack1View1_Click(sender As Object, e As EventArgs) Handles btnBack1View1.Click
        multiview1.ActiveViewIndex = 0
    End Sub
End Class